﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TournamentDatabase.Controls
{
	/// <summary>
	/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
	///
	/// Step 1a) Using this custom control in a XAML file that exists in the current project.
	/// Add this XmlNamespace attribute to the root element of the markup file where it is 
	/// to be used:
	///
	///     xmlns:MyNamespace="clr-namespace:TournamentDatabase.Controls"
	///
	///
	/// Step 1b) Using this custom control in a XAML file that exists in a different project.
	/// Add this XmlNamespace attribute to the root element of the markup file where it is 
	/// to be used:
	///
	///     xmlns:MyNamespace="clr-namespace:TournamentDatabase.Controls;assembly=TournamentDatabase.Controls"
	///
	/// You will also need to add a project reference from the project where the XAML file lives
	/// to this project and Rebuild to avoid compilation errors:
	///
	///     Right click on the target project in the Solution Explorer and
	///     "Add Reference"->"Projects"->[Browse to and select this project]
	///
	///
	/// Step 2)
	/// Go ahead and use your control in the XAML file.
	///
	///     <MyNamespace:BackgroundTextBox/>
	///
	/// </summary>
	public class BackgroundTextBox : TextBox
	{
		VisualBrush m_bgVisualBrush;
		TextBlock m_bgTextBlock;

		public string BackgroundText
		{
			get { return m_bgTextBlock.Text; }
			set { m_bgTextBlock.Text = value; }
		}

		public BackgroundTextBox()
		{
			m_bgVisualBrush = new VisualBrush();
			m_bgVisualBrush.TileMode = TileMode.None;
			m_bgVisualBrush.Opacity = 0.4;
			m_bgVisualBrush.Stretch = Stretch.None;
			m_bgVisualBrush.AlignmentX = AlignmentX.Left;

			m_bgTextBlock = new TextBlock();
			m_bgTextBlock.FontStyle = FontStyles.Italic;
			m_bgTextBlock.Text = "";

			m_bgVisualBrush.Visual = m_bgTextBlock;

			this.TextChanged += new TextChangedEventHandler(BackgroundTextBox_TextChanged);

			BackgroundTextBox_TextChanged(null, null);
		}

		void BackgroundTextBox_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (this.Text == null || this.Text == "")
			{
				this.Background = m_bgVisualBrush;
			}
			else
			{
				this.Background = null;
			}
		}
	}
}
